package br.com.mrh.bean;

import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean
@SessionScoped
public class LinguaBean extends BaseBean {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private Locale currentLocale;

	private static Map<String, Object> countries;

	static {
		countries = new LinkedHashMap<String, Object>();
		countries.put("Portuguese", Locale.getDefault());
		countries.put("English", new Locale("en", "US"));
	}

	public Map<String, Object> getCountriesInMap() {
		return countries;
	}
	
	public Locale getCurrentLocale() {
		return currentLocale;
	}

	public void setCurrentLocale(Locale currentLocale) {
		this.currentLocale = currentLocale;
	}
	
	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void verificaLingua() {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * Busca se local e verifica se alteracao e permitida
	 * @param  valorLocal
	 * @return Locale
	 */
	private Locale trocaLingua(String valorLocal) {
		
		for (Map.Entry<String, Object> entry : countries.entrySet()) {
			
			Locale local = (Locale) entry.getValue();
			
			if (local.getCountry().toString().equals(valorLocal.toUpperCase())) {

				return (Locale) entry.getValue();

			}

		}

		return Locale.getDefault();

	}
	
	/**
	 * Altera local para Brasil
	 */
	public void alterarLinguaBR(){
		
		setCurrentLocale(trocaLingua("BR"));
		
		FacesContext.getCurrentInstance().getViewRoot()
		.setLocale(getCurrentLocale());
	}
	
	/**
	 * Altera local para United States
	 */
	public void alteraLinguaUS(){
		
		setCurrentLocale(trocaLingua("US"));
		
		FacesContext.getCurrentInstance().getViewRoot()
		.setLocale(getCurrentLocale());
	}

}
